﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AFI.Speargun.Laptop.ServiceReference;

namespace AFI.Speargun.Laptop
{
    public partial class MainForm : Form
    {
        ServiceClient _proxy;
        Guid _inventoryCheckingId;

        public MainForm()
        {
            InitializeComponent();

            _proxy = new ServiceClient();
            _inventoryCheckingId = Guid.Empty;
        }

        ~MainForm()
        {
            _proxy.Close();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(_proxy.ClientCredentials.UserName.UserName))
            {
                _proxy.ClientCredentials.UserName.UserName = username.Text;
                _proxy.ClientCredentials.UserName.Password = password.Text;
            }

            bindingSource1.DataSource = _proxy.ListInventoryCheckings();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (IsANonHeaderLinkCell(dataGridView1, e))
            {
                _inventoryCheckingId = new Guid(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());

                bindingSource2.DataSource = _proxy.ListInventoryChecking(_inventoryCheckingId);
            }
        }

        private bool IsANonHeaderLinkCell(DataGridView dataGridView, DataGridViewCellEventArgs cellEvent)
        {
            if (dataGridView.Columns[cellEvent.ColumnIndex] is
                DataGridViewLinkColumn &&
                cellEvent.RowIndex != -1)
            { return true; }
            else { return false; }
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (IsANonHeaderLinkCell(dataGridView2, e))
            {
                Guid assetEntryId = new Guid(dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString());

                var checkingForm = new CheckingForm(_proxy, _inventoryCheckingId, assetEntryId);
                checkingForm.ShowDialog();
            }
        }
    }
}
